﻿using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using YFrameWork.RunTime;


namespace YFrameWork.Editor
{

    [CreateAssetMenu(fileName = "ChangeFontObject", menuName = "About/CreateAssetData/ChangeFontObject")]
    public class ChangeFontObject : SerializedScriptableObject
    {
        [LabelText("需要改变的字体的字典")]
        [Searchable]
        //[TableList]
        [PropertyOrder(2)]
        public List<GoList> Go_Font = new List<GoList>();
    }


    [Serializable]
    public class GoList
    {
        private const string VerticalGroup = "描述";
        private const string VerticalGroup_Assets = "资源列表";

        [LabelText("字体")]
        [Required("请至少选择一个字体")]
        [AssetSelector]
        [PropertyOrder(1)]
        public Font Font;

        [AssetsOnly]
        [LabelText("预制体列表")]
        [PropertyOrder(2)]
        public List<GameObject> GOS;



        [Button("改变字体", ButtonSizes.Medium)]
        [PropertyOrder(0)]
        public void ChangeFont()
        {
            try
            {
                if (Font == null)
                {
                    Log.Error("为列表选择的字体是空的！");
                    return;
                }
                int GosProgress = 0;
                foreach (var item in GOS)
                {
                    string path = AssetDatabase.GetAssetPath(item);
                    GameObject go = PrefabUtility.LoadPrefabContents(path);

                    List<Transform> childTrans = ChangeFontTable.GetChildTransfrom(go.transform);
                    GosProgress++;
                    int ChildProgress = 0;
                    foreach (var childtran in childTrans)
                    {
                        ChangeFontTable.ChangeTextFont(childtran, Font);

                        EditorUtility.DisplayProgressBar($"正在替换:{childtran.name}", $"进度{ChildProgress}/{childTrans.Count}", ChildProgress / childTrans.Count);
                        ChildProgress++;
                    }
                    ChangeFontTable.ChangeTextFont(go.transform, Font);
                    EditorUtility.DisplayProgressBar($"正在替换:{go.name}", $"进度{GosProgress}/{GOS.Count}", GosProgress / GOS.Count);

                    PrefabUtility.SaveAsPrefabAsset(go, path);
                    PrefabUtility.UnloadPrefabContents(go);
                }
                Log.Debug("字体{0}替换成功", Font.name);

            }
            catch (Exception e)
            {

                Log.Error("字体{0}替换失败,原因是：{1}", Font.name, e);
            }
            EditorUtility.ClearProgressBar();

        }
    }

}